<template>
  <el-dialog :visible="isShow" title="添加员工" width="500" @close="closeEvent">
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="form.timeOfEntry"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="ormOfEmployment">
        <el-select v-model="form.formOfEmployment">
          <el-option
            v-for="item in employees.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <div ref="tree">
          <el-input
            v-model="form.departmentName"
            placeholder="请输入"
            @focus="focusEvent"
          />
          <el-tree
            v-if="showTree"
            readonly
            :data="treeData"
            :props="{ label: 'name' }"
            @node-click="nodeClick"
          />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="form.correctionTime"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="footer">
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button type="primary" @click="closeEvent">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import employees from '@/assets/constant/employees.js'
import { companyDepartment } from '@/api/departments.js'
import { sysUserPost } from '@/api/employees.js'
export default {
  name: 'DiaLogItem',
  model: {
    prop: 'isShow'
  },
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      showTree: false,
      treeData: [],
      employees: employees,
      form: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }],
        mobile: [{ required: true, message: '必填', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '必填', trigger: 'change' }],
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        workNumber: [{ required: true, message: '必填', trigger: 'change' }],
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        correctionTime: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  mounted() {
    document.addEventListener('click', this.clickEvent)
  },
  beforeCreate() {
    document.removeEventListener('click', this.clickEvent)
  },
  methods: {
    //  关闭弹框
    closeEvent() {
      this.$emit('input', false)
    },
    //  提交
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          await sysUserPost(this.form)
          this.$message.success('添加成功')
          this.closeEvent()
          this.$emit('getData')
        }
      })
    },
    //  聚焦获取数据
    async focusEvent() {
      this.showTree = true
      if (this.treeData.length === 0) {
        const res = await companyDepartment()
        this.treeData = this.changeData(res.data.depts)
        console.log('treeData', this.treeData)
        console.log('数据素具', res)
      }
    },
    changeData(arr, str = '') {
      return arr.filter((item) => {
        if (item.pid === str) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    //  点击的每一项
    nodeClick(data) {
      //   console.log(data)
      this.form.departmentName = data.name
    },
    // 监听点击范围
    clickEvent(e) {
      if (this.isShow && !this.$refs.tree.contains(e.target)) {
        this.showTree = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.footer {
  text-align: center;
}
</style>
